package meterMonitor.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.mail.util.LineOutputStream;
import meterMonitor.biz.*;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.*;
import meterMonitor.util.BizUtil;
import meterMonitor.util.Timer.StartAndEndTime;
import meterMonitor.util.excel.ExcelUtil;
import meterMonitor.util.excel.domain.ExcelColumn;
import meterMonitor.util.excel.domain.ExcelHead;
import meterMonitor.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表
 */
@RequestMapping("report")
@RestController
public class ReportController extends BaseController {
    public static final Logger logger = LoggerFactory.getLogger(ReportController.class);

    @Autowired
    private MonitorBiz monitorBiz;
    @Autowired
    private MeterBiz meterBiz;

    @Autowired
    private MonitorDetailBiz monitorDetailBiz;
    @Autowired
    private MonitorDayBiz monitorDayBiz;
    @Autowired
    private MonitorMonthBiz monitorMonthBiz;
    @Autowired
    private MonitorYearBiz monitorYearBiz;
    @Autowired
    private MonitorWeekBiz monitorWeekBiz;


    @Autowired
    BizUtil bizUtil;

    /**
     * 报表首页展示
     *
     * @param mv
     * @return
     */
    @RequestMapping("index.do")
    @FireAuthority(authorityTypes = AuthorityType.Query)
    private ModelAndView index(ModelAndView mv) {
        mv.addObject("initialDate", bizUtil.getCouldShowDate());
        return mv;
    }

    /**
     * 查询报表信息
     *
     * @param repType
     * @param fromTime
     * @param toTime
     * @param meterCode
     * @return
     */
    @RequestMapping("queryReports.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.Query)
    public BaseResponseData queryReports(Integer repType, String fromTime, String toTime,String areaId, String deviceTypeCode, String meterCode) {
        BaseResponseData response = new BaseResponseData();


        //允许设备类型为空
//        if(StringUtils.isBlank(deviceTypeCode)){
//            response.setCode(ResponseCode.DEFAULT_EXCEPTION);
//            response.setMessage("设备类型不能为空");
//            return response;
//        }


        int type=Integer.parseInt(deviceTypeCode);//将string类型转换为int
        System.out.println(type);
        System.out.println("仪器类型是："+deviceTypeCode);
        System.out.println(areaId+"0000000");
        //将string类型进行拆分拼接："A,B"->"'A','B'"

        if (StringUtils.isBlank(areaId)){
            response.setCode(ResponseCode.DEFAULT_EXCEPTION);
            response.setMessage("请选择查询区域");
            return response;
        }
        try {
            if (StringUtils.isBlank(fromTime) || StringUtils.isBlank(meterCode)) {
                response.setCode(ResponseCode.DEFAULT_EXCEPTION);
                response.setMessage("请选择查询时间和设备！");
                return response;
            }
            //获取当前时间
            Calendar calendar = Calendar.getInstance();

            switch (repType) {
                case 1:
                    //明细表数据
                    fromTime = fromTime + " 00";
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
                    calendar.setTime(simpleDateFormat.parse(fromTime));
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    List<MonitorDetail> list1 = monitorDetailBiz.queryData(simpleDateFormat.format(simpleDateFormat.parse(fromTime)), simpleDateFormat.format(calendar.getTime()), meterCode);
                    response.setData(list1);
                    break;
                case 2:
                    //日报数据
                    /**
                     *  "pkid" : 461,
                     *     "meterCode" : 68,
                     *     "intervalQuantity" : 3.51,
                     *     "uploadTime" : 1629129225000,
                     *     "day" : "2021-08-16",
                     *     "standard" : 30.0,
                     *     "deviceName" : "1003",
                     *     "anotherName" : "小办公室03",
                     *     "areaName" : "默认放这里",
                     *     "fromTime" : null,
                     *     "toTime" : null,
                     *     "meterCodes" : null,
                     *     "isOver" : null,
                     *     "currentQuantiry" : null
                     */
                    fromTime = fromTime + "-01";
                    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
                    calendar.setTime(simpleDateFormat1.parse(fromTime));
                    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
                    List<MonitorDay> list = monitorDayBiz.queryDataByTypeWithNoAreaId(simpleDateFormat1.format(simpleDateFormat1.parse(fromTime)), simpleDateFormat1.format(calendar.getTime()),areaId,type, meterCode);

                    List<String > setMeterCode1=new ArrayList<>();
                    List<String > getMeterCode1=new ArrayList<>();
                    for (MonitorDay monitorDay : list) {
                        setMeterCode1.add(String.valueOf(monitorDay.getMeterCode()));
                    }
                    String[] temp = meterCode.split(",");
                    for (int i = 0; i < temp.length; i++) {
                            getMeterCode1.add(temp[i]);
                    }
                    Collection exists=new ArrayList<String>(getMeterCode1);
                    exists.removeAll(setMeterCode1);
                    String ab=null;
                    List<MonitorDay> list2=new ArrayList<>();
                    for (Object exist : exists) {
                         ab=(String) exist;
                        list2= monitorDayBiz.queryDataByTypeWithNoAreaId1(areaId, type, ab);
                        for (MonitorDay monitorDay : list2) {
                            list.add(monitorDay);
                        }
                    }


                    response.setData(list);
                    break;
                case 3:
                    //周报数据
                    Calendar instance = Calendar.getInstance();
                    String week = instance.get(Calendar.YEAR) + (instance.get(Calendar.WEEK_OF_YEAR) < 10 ? "0" + instance.get(Calendar.WEEK_OF_YEAR) : instance.get(Calendar.WEEK_OF_YEAR) + "");
                    List<MonitorWeek> monitorWeeks = monitorWeekBiz.queryDataByWeek(fromTime + "01", week,areaId,type,meterCode);
                    response.setData(monitorWeeks);
                    break;
                case 4:
                    //月报数据
                    fromTime = fromTime + "-01";
                    toTime = toTime + "-12";
                    List<MonitorMonth> monitorDays = monitorMonthBiz.queryDataByMonth(fromTime, toTime,areaId,type, meterCode);
                    response.setData(monitorDays);
                    break;
                case 5:
                    //年报数据
                    SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy");
                    List<MonitorYear> monitorYears = monitorYearBiz.queryData(simpleDateFormat3.format(simpleDateFormat3.parse(fromTime)), simpleDateFormat3.format(simpleDateFormat3.parse(toTime)), meterCode);
                    response.setData(monitorYears);
                    break;
            }
            response.setCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            response.setCode(ResponseCode.DEFAULT_EXCEPTION);
            response.setMessage(e.getMessage() != null ? e.getMessage() : "系统异常！请联系管理员");
        }
        return response;
    }


    private String DAY_CODE = "10";
    private String NIGHT_CODE = "20";

    @RequestMapping("queryReport.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.Query)
    private JSONObject queryReport(Integer repType, String fromTime, String toTime, String meterCode) {
        JSONObject resultData = new JSONObject();
        try {
            // 校验参数
            if (StringUtils.isBlank(fromTime) || StringUtils.isBlank(toTime) || StringUtils.isBlank(meterCode)) {
                resultData.put("statusCode", "ERROR");
                resultData.put("errMsg", "请选择查询时间和设备！");
                return resultData;
            }

            //校验日期 结束时间不能早于开始时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date to = sdf.parse(toTime);
            Date fo = sdf.parse(fromTime);
            if (to.getTime() < fo.getTime()) {
                resultData.put("statusCode", "ERROR");
                resultData.put("msg", "结束时间不能早于开始时间！");
                return resultData;
            }

            //校验日期 不能跨月查询
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
            String f = sdf1.format(sdf.parse(fromTime));
            String t1 = sdf1.format(sdf.parse(toTime));
            if (!f.equals(t1)) {
                resultData.put("msg", "查询开始时间结束时间需在同一个月");
                return resultData;
            }

            //校验时间 大于当前时间 取当前时间
            String currentTime = StartAndEndTime.getCurrentTime();
            if (sdf.parse(toTime).getTime() > sdf.parse(currentTime).getTime()) {
                toTime = StartAndEndTime.getTheDayBefore();
            }
            if (sdf.parse(fromTime).getTime() > sdf.parse(currentTime).getTime()) {
                fromTime = StartAndEndTime.getTheDayBefore();
            }

            String[] device_codes = meterCode.split(",");
            List listData = new ArrayList();
            List listColumn = new LinkedList<FrontColumn>();
            String repTitle = "";
            switch (repType) {
                case 1:
                    //明细小时报=> 查询指定天内的明细小时报
                    repTitle = fromTime + "~" + toTime + " 明细报表";
                    List<MonitorDetail> list = monitorDetailBiz.queryData(fromTime, toTime, meterCode);
                    listData = list;
                    listColumn = getColumnsReportHourly();
                    break;
                case 2:
                    //查询班次
                    repTitle = fromTime + "~" + toTime + " 班次报表";
                    List<ReportShiftly> list2 = monitorBiz.getReportShiftly(fromTime, toTime, device_codes);
                    for (ReportShiftly r : list2) {
                        if (r.getOverFlow()) {
                            listData.add(r);
                        }
                    }
                    listData = list2;
                    listColumn = getColumnsReportShiftly();
                    break;
                case 3:
                    //日报=》查指定天内的白班+晚班的日报
                    repTitle = fromTime + "~" + toTime + " 设备日报";
                    List<ReportDaily> list3 = monitorBiz.getReportDaily(fromTime, toTime, null, device_codes);
                    for (ReportDaily r : list3) {
                        if (r.getOverFlow()) {
                            listData.add(r);
                        }
                    }
                    listData = list3;
                    listColumn = getColumnsReportDaily();
                    break;
                case 4:
                    //月报=》查指定天内的月报
                    repTitle = fromTime + "~" + toTime + " 设备月报";
                    List<ReportMonthly> list4 = monitorBiz.getReportMonthly(fromTime, toTime, device_codes);
                    for (ReportMonthly r : list4) {
                        if (r.getOverFlow()) {
                            listData.add(r);
                        }
                    }
                    listData = list4;
                    listColumn = getColumnsReportMonthly();
                    break;
            }
            //
            resultData.put("repTitle", repTitle + "-统计表");
            resultData.put("statusCode", "OK");
            resultData.put("data", listData);
            resultData.put("column", listColumn);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return resultData;
    }

    @RequestMapping("exportReport.do")
    @ResponseBody
    public void exportReport(HttpServletRequest request, HttpServletResponse response) {
        String objRepType = request.getParameter("repType");
        String fromTime = request.getParameter("fromTime");
        String toTime = request.getParameter("toTime");
        String metorCodes = request.getParameter("meterCodes");
        Integer repType = Integer.valueOf(objRepType);

        //
        JSONObject resultData = queryReport(repType, fromTime, toTime, metorCodes);
        List listData = (List) resultData.get("data");
        Map<Long, MonitorDetailExcel> datas = new LinkedHashMap<Long, MonitorDetailExcel>();
        for (Object data : listData) {
            MonitorDetail detail = (MonitorDetail) data;
            long code = detail.getMeterCode();
            MonitorDetailExcel monitorDetailExcel = datas.get(code);
            if (monitorDetailExcel == null) {
                monitorDetailExcel = new MonitorDetailExcel();
                monitorDetailExcel.setAreaName(detail.getAreaName());
                monitorDetailExcel.setAnotherName(detail.getAnotherName());
                monitorDetailExcel.setDeviceName(detail.getDeviceName());
                monitorDetailExcel.setStandard(detail.getStandard());
                setHx(monitorDetailExcel, Integer.parseInt(detail.getDetailDate().substring(11, 13)), detail.getIntervalQuantity());
                datas.put(code, monitorDetailExcel);
            } else {
                setHx(monitorDetailExcel, Integer.parseInt(detail.getDetailDate().substring(11, 13)), detail.getIntervalQuantity());
            }
        }

        LinkedList<FrontColumn> listColumn = (LinkedList<FrontColumn>) resultData.get("column");
        String repTitle = (String) resultData.get("repTitle");

        //设置表头
        List<ExcelColumn> columns = new ArrayList<>();
        for (int i = 0; i < listColumn.size(); i++) {
            columns.add(new ExcelColumn(i, listColumn.get(i).getField(), listColumn.get(i).getTitle()));
        }
        ExcelHead head = new ExcelHead();
        head.setColumns(columns);
        //生成Excel文件流
        ExcelUtil.getInstanse().exportExcel(head, repTitle.replaceAll(":", "-"), Arrays.asList(datas.values().toArray()), response);
    }

    private void setHx(MonitorDetailExcel monitorDetailExcel, int hour, double value) {
        switch (hour) {
            case 0:
                monitorDetailExcel.setH0(value);
                break;
            case 1:
                monitorDetailExcel.setH1(value);
                break;
            case 2:
                monitorDetailExcel.setH2(value);
                break;
            case 3:
                monitorDetailExcel.setH3(value);
                break;
            case 4:
                monitorDetailExcel.setH4(value);
                break;
            case 5:
                monitorDetailExcel.setH5(value);
                break;
            case 6:
                monitorDetailExcel.setH6(value);
                break;
            case 7:
                monitorDetailExcel.setH7(value);
                break;
            case 8:
                monitorDetailExcel.setH8(value);
                break;
            case 9:
                monitorDetailExcel.setH9(value);
                break;
            case 10:
                monitorDetailExcel.setH10(value);
                break;
            case 11:
                monitorDetailExcel.setH11(value);
                break;
            case 12:
                monitorDetailExcel.setH12(value);
                break;
            case 13:
                monitorDetailExcel.setH13(value);
                break;
            case 14:
                monitorDetailExcel.setH14(value);
                break;
            case 15:
                monitorDetailExcel.setH15(value);
                break;
            case 16:
                monitorDetailExcel.setH16(value);
                break;
            case 17:
                monitorDetailExcel.setH17(value);
                break;
            case 18:
                monitorDetailExcel.setH18(value);
                break;
            case 19:
                monitorDetailExcel.setH19(value);
                break;
            case 20:
                monitorDetailExcel.setH20(value);
                break;
            case 21:
                monitorDetailExcel.setH21(value);
                break;
            case 22:
                monitorDetailExcel.setH22(value);
                break;
            case 23:
                monitorDetailExcel.setH23(value);
                break;
        }
    }

    private List getColumnsReportHourly() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("deviceName");
        c1.setTitle("设备");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c2 = new FrontColumn();
        c2.setField("standard");
        c2.setTitle("标准用量");
        c2.setSort(true);
        list.add(c2);

        FrontColumn c3;
        for (int i = 0; i < 24; i++) {
            c3 = new FrontColumn();
            c3.setField("h" + String.valueOf(i));
            if (i < 10) {
                c3.setTitle("0" + i + ":00");
            } else {
                c3.setTitle(i + ":00");
            }
            list.add(c3);
        }
        return list;
    }

    private List getColumnsReportHourlySum() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("dateCode");
        c1.setTitle("时间");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);

        FrontColumn c3 = new FrontColumn();
        c3.setSort(true);
        c3.setField("deviceName");
        c3.setTitle("仪表位置");
        list.add(c3);
        //
        FrontColumn c4 = new FrontColumn();
        c4.setField("ownerTimer");
        c4.setTitle("时间");
        list.add(c4);
        //
        // FrontColumn c5 = new FrontColumn();
        // c5.setField("standardUsageShow");
        // c5.setTitle("标准用量(T)");
        // //c5.setTotalRow(true);
        // list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        // FrontColumn c7 = new FrontColumn();
        // c7.setField("saveRateShow");
        // c7.setTitle("节约率");
        // list.add(c7);
        //
        // FrontColumn c8 = new FrontColumn();
        // c8.setField("overFlowName");
        // c8.setTitle("是否超标");
        // list.add(c8);
        //
//        FrontColumn c9 = new FrontColumn();
//        c9.setField("exceedHourShow");
//        c9.setTitle("超标时长(H)");
//        //c9.setTotalRow(true);
//        list.add(c9);
        //
//        FrontColumn c33 = new FrontColumn();
//        c33.setField("electricQtyShow");
//        c33.setTitle("电池耗量");
//        list.add(c33);
        //
//        FrontColumn c10 = new FrontColumn();
        //       c10.setField("currentQuantiry");
        //       c10.setTitle("水表数值(T)");
        //       //c9.setTotalRow(true);
        //       list.add(c10);
        return list;
    }

    private List getColumnsReportShiftly() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("dateCode");
        c1.setTitle("日期");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c3 = new FrontColumn();
        c3.setSort(true);
        c3.setField("deviceName");
        c3.setTitle("仪表位置");
        list.add(c3);
        //
        FrontColumn c9 = new FrontColumn();
        c9.setField("dayOrNight");
        c9.setTitle("班次");
        list.add(c9);

        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        // FrontColumn c10 = new FrontColumn();
        // c10.setField("currentQuantiry");
        // c10.setTitle("水表数值(T)");
        // list.add(c10);
        return list;
    }

    private List getColumnsReportShiftlySum() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("dateCode");
        c1.setTitle("日期");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c9 = new FrontColumn();
        c9.setField("dayOrNight");
        c9.setTitle("班次");
        list.add(c9);

        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        return list;
    }

    private List getColumnsReportDaily() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("dateCode");
        c1.setTitle("日期");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c3 = new FrontColumn();
        c3.setSort(true);
        c3.setField("deviceName");
        c3.setTitle("仪表位置");
        list.add(c3);
        //

        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        // FrontColumn c10 = new FrontColumn();
        // c10.setField("currentQuantiry");
        // c10.setTitle("水表数值(T)");
        // list.add(c10);
        return list;
    }

    private List getColumnsReportDailySum() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("dateCode");
        c1.setTitle("日期");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        return list;
    }

    private List getColumnsReportMonthly() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("monthCode");
        c1.setTitle("月份");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //
        FrontColumn c3 = new FrontColumn();
        c3.setSort(true);
        c3.setField("deviceName");
        c3.setTitle("仪表位置");
        list.add(c3);
        //

        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        // FrontColumn c10 = new FrontColumn();
        // c10.setField("currentQuantiry");
        // c10.setTitle("水表数值(T)");
        // //c9.setTotalRow(true);
        // list.add(c10);
        return list;
    }

    private List getColumnsReportMonthlySum() {
        List<FrontColumn> list = new LinkedList<FrontColumn>();
        //
        FrontColumn c1 = new FrontColumn();
        c1.setField("monthCode");
        c1.setTitle("月份");
        c1.setSort(true);
        c1.setTotalRowText("合计");
        list.add(c1);
        //

        FrontColumn c5 = new FrontColumn();
        c5.setField("standardUsageShow");
        c5.setTitle("标准用量(T)");
        //c5.setTotalRow(true);
        list.add(c5);
        //
        FrontColumn c6 = new FrontColumn();
        c6.setField("realUsageShow");
        c6.setTitle("实际用量(T)");
        //c6.setTotalRow(true);
        list.add(c6);
        //
        FrontColumn c7 = new FrontColumn();
        c7.setField("saveRateShow");
        c7.setTitle("节约率");
        list.add(c7);
        //
        FrontColumn c8 = new FrontColumn();
        c8.setField("overFlowName");
        c8.setTitle("是否超标");
        list.add(c8);
        //
        return list;
    }


    //查所有在线
    @RequestMapping("getAllMeters.do")
    @ResponseBody
    private List<Meter> getAllMeters() {

        List<Meter> list = new LinkedList<>();
        try {
            list = meterBiz.queryAllMeter();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }

    //有用：按设备类型查所有不分状态

    @RequestMapping("getAllMetersNoStatus.do")
    @ResponseBody
    private List<Meter> getAllMetersNoStatus(String deviceTypeCode) {

        int type=Integer.parseInt(deviceTypeCode);

        List<Meter> list = new LinkedList<>();
        try {
            list = meterBiz.queryAllMeterNoStatus(type);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return list;
    }



    /**
     * 根据仪器类型查出所有表
     * @param type
     * @return
     */
    @RequestMapping("getAllMetersByType.do")
    @ResponseBody
    private List<Meter> getAllMeterByType(int type){
        List<Meter> meterList=new LinkedList<>();
        //水表
        if (type!=0){

            try {
                meterList=meterBiz.queryAllMeter();
            } catch (Exception e){
                logger.error(e.getMessage(),e);
            }
        }
        else if(type==0){
            try {
                meterList=meterBiz.queryAllMeterElectricity();
            } catch (Exception e){
                logger.error(e.getMessage(),e);
            }
        }
        return meterList;
    }


    public void exportDIYRep(HttpServletRequest request, HttpServletResponse response) {

        String fromTime = request.getParameter("fromDate");
        String toTime = request.getParameter("toDate");

        //
        List<Map<String, Object>> listData = monitorBiz.queryDIYRep(fromTime, toTime);

        //导出功能
        List<ExcelColumn> columns = monitorBiz.headersOfDIYRep();
        //
        ExcelHead head = new ExcelHead();
        head.setColumns(columns);
        //生成Excel文件流
        ExcelUtil.getInstanse().exportExcelWithMergeHeaderRow(head, "水表每日用水量报表", listData, response);
    }

    @RequestMapping("exportDIYRep.do")
    @ResponseBody
    public void exportDIYRep2(HttpServletRequest request, HttpServletResponse response) {
        String fromTime = request.getParameter("fromDate");
        String toTime = request.getParameter("toDate");
        //
        monitorBiz.queryDIYRep3(fromTime, toTime, response);
    }

}
